package com.java.zhuiyun.api.syslog.service.impl;

import com.java.zhuiyun.api.syslog.entity.SysLog;
import com.java.zhuiyun.api.syslog.mapper.SysLogMapper;
import com.java.zhuiyun.api.syslog.model.SysLogModel;
import com.java.zhuiyun.api.syslog.service.SysLogService;
import com.java.zhuiyun.common.errorCode.CommonException;
import com.java.zhuiyun.common.errorCode.EmErrorCode;
import com.java.zhuiyun.common.response.CommonResult;
import com.java.zhuiyun.util.BeanUtils;
import com.java.zhuiyun.util.ObjectUtils;
import com.java.zhuiyun.util.pageUtil.MybatisPageHelper;
import com.java.zhuiyun.util.pageUtil.PageRequest;
import com.java.zhuiyun.util.pageUtil.PageResult;
import com.java.zhuiyun.util.validator.ValidationResult;
import com.java.zhuiyun.util.validator.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @auther zcy
 * @data 2023/3/22 13:35
 */
@Service
public class SysLogServiceImpl  implements SysLogService {

    @Autowired
    SysLogMapper sysLogMapper;

    /**
     * 分页查询
     * @param pageRequest
     * @param sysLogModel
     * @return
     */
    @Override
    public PageResult selectSysLogByPage(PageRequest pageRequest, SysLogModel sysLogModel) {
        MybatisPageHelper.startPage(pageRequest);
        return MybatisPageHelper.getPageResult(sysLogMapper.selectSysLogByPage(sysLogModel));
    }

    /**
     * 删除日志
     * @param id
     * @return
     * @throws CommonException
     */
    @Override
    public CommonResult deleteSysLogById(Integer id) throws CommonException {
        // 判断是否id为空
        if(ObjectUtils.isNotEmpty(id)){
            int i = sysLogMapper.deleteByPrimaryKey(id);
            // 判断是否操作成功
            if (i <= 0) {
                throw new CommonException(EmErrorCode.DELETE_ERROR);
            }
        }else{
            throw new CommonException(EmErrorCode.ID_NULL);
        }
        return CommonResult.ok(EmErrorCode.DELETE_OK);
    }

    @Override
    public SysLog selectSysLogById(Integer id) throws CommonException {
        // 判断是否id为空
        if(ObjectUtils.isNotEmpty(id)){
            SysLog sysLog = sysLogMapper.selectByPrimaryKey(id);
            // 判断是否操作成功
            if (ObjectUtils.isNotEmpty(sysLog)) {
                return sysLog;
            }else{
                throw new CommonException(EmErrorCode.ID_NOT_OBJECT);
            }
        }else{
            throw new CommonException(EmErrorCode.ID_NULL);
        }
    }

    /**
     * 根据时间段查询日志
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public Integer selectSysLogByTime(String startTime, String endTime) {
        Integer integer = sysLogMapper.selectSysLog(startTime,endTime);
        return integer;
    }

    /**
     * 获取用户时间范围内操作日志的次数
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public List<Map<String,Object>> getLogAnalysis(String startTime, String endTime) {
        List<Map<String, Object>> list = sysLogMapper.selectUserOperate(startTime,endTime);
        return list;
    }

    /**
     * 获取时间内所有用户不同ip的登录次数
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public List<Map<String, Object>> getLoginIpCount(String startTime, String endTime) {
        List<Map<String, Object>> loginIpcount = sysLogMapper.getLoginIpCount(startTime,endTime);
        return loginIpcount;
    }

    /**
     * 保存日志
     * @param sysLogModel
     * @return
     * @throws CommonException
     */
    @Override
    public CommonResult saveSyslog(SysLogModel sysLogModel) throws CommonException {
        if(ObjectUtils.isNotEmpty(sysLogModel)){
            ValidationResult result = ValidatorUtil.validate(sysLogModel);
            if (result.isHasError()) {
                throw new CommonException(EmErrorCode.PARAMETER_VALIDATION_ERROR, result.getErrMsg());
            }
            SysLog sysLog = new SysLog();
            // sysLogModel 对象拷贝成 sysLog对象中
            BeanUtils.copy(sysLogModel, sysLog);
            int i = sysLogMapper.insert(sysLog);
            if(i>0){
                return CommonResult.ok(EmErrorCode.SAVE_OK);
            }else{
                return CommonResult.ok(EmErrorCode.SAVE_ERROR);
            }
        }else{
            // 返回前端异常数据
            throw new CommonException(EmErrorCode.OBJECT_NOT_NULL);
        }
    }
}
